﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;

public partial class Charts_Stack_CenterStandard : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CenterStandardChart.Titles[0].Text = string.Format("نمودار مصرف انرژی {1} مرکز {0}", CenterDropDownList.SelectedItem, TypeDropDownList.SelectedItem);         
    }
    protected void ChartLinkButton_Click(object sender, EventArgs e)
    {
        CenterStandardSqlDataSource.DataBind();
        Series minSeries = new Series();
        minSeries.Name = "کم مصرف";
        minSeries.ChartType = SeriesChartType.StackedColumn;
        minSeries.Color = Color.Yellow;

        Series maxSeries = new Series();
        maxSeries.Name = "مصرف نرمال";
        maxSeries.ChartType = SeriesChartType.StackedColumn;
        maxSeries.Color = Color.Green;

        Series minmaxSeries = new Series();
        minmaxSeries.Name = "پر مصرف";
        minmaxSeries.ChartType = SeriesChartType.StackedColumn;
        minmaxSeries.Color = Color.Red;

        DataView dataView = new DataView();
        DataTable dataTable = new DataTable();
        dataView = CenterStandardSqlDataSource.Select(DataSourceSelectArguments.Empty) as DataView;
        if (dataView != null)
        {
            dataTable = dataView.ToTable();
            int maxValue = int.MinValue;
            int currentValue = 0;
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                currentValue = int.Parse(dataTable.Rows[i].ItemArray[1].ToString());
                if (currentValue > maxValue)
                    maxValue = currentValue;
            }
            maxValue *= 2;
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                minSeries.Points.AddXY(string.Format("{0}-{1}", PersianDateControls.Convertor.ToPersianDate((DateTime)dataTable.Rows[i].ItemArray[2]), PersianDateControls.Convertor.ToPersianDate((DateTime)dataTable.Rows[i].ItemArray[3])), dataTable.Rows[i].ItemArray[0]);
                maxSeries.Points.AddXY(string.Format("{0}-{1}", PersianDateControls.Convertor.ToPersianDate((DateTime)dataTable.Rows[i].ItemArray[2]), PersianDateControls.Convertor.ToPersianDate((DateTime)dataTable.Rows[i].ItemArray[3])), int.Parse(dataTable.Rows[i].ItemArray[1].ToString()) - int.Parse(dataTable.Rows[i].ItemArray[0].ToString()));
                minmaxSeries.Points.AddXY(string.Format("{0}-{1}", PersianDateControls.Convertor.ToPersianDate((DateTime)dataTable.Rows[i].ItemArray[2]), PersianDateControls.Convertor.ToPersianDate((DateTime)dataTable.Rows[i].ItemArray[3])), maxValue - int.Parse(dataTable.Rows[i].ItemArray[1].ToString()));
            }
            CenterStandardChart.Series.Add(minSeries);
            CenterStandardChart.Series.Add(maxSeries);
            CenterStandardChart.Series.Add(minmaxSeries);

            CenterStandardChart.DataBind();
        }
    }
}